package com.zjbti.countducks;

import androidx.appcompat.app.AppCompatActivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private MyBroadcastReceiveOne one;
    private MyBroadcastReceiveTwo two;
    private MyBroadcastReceiveThree three;
    private ImageView iv_horn;
    private TextView tv_left_content,tv_one,tv_two,tv_three;
    private int num=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        registerReceiver();
        init();
    }

    private void init() {
        iv_horn=findViewById(R.id.iv_horn);
        tv_left_content=findViewById(R.id.tv_left_content);
        tv_one=findViewById(R.id.tv_one);
        tv_two=findViewById(R.id.tv_two);
        tv_three=findViewById(R.id.tv_three);
        iv_horn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv_left_content.setVisibility(View.VISIBLE);
                iv_horn.setClickable(false);
                Intent intent=new Intent();
                intent.setAction("Count_Ducks");
                sendOrderedBroadcast(intent,null);
            }
        });
    }

    class MyBroadcastReceiveOne extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent){
            tv_one.setVisibility(View.VISIBLE);
            num=num+1;
            tv_one.setText(num+"");
            Log.i("BroadcastReceiveOne","广播接收者One，收到了广播消息");
            delay();
        }
    }
    class MyBroadcastReceiveTwo extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent){
            tv_two.setVisibility(View.VISIBLE);
            num=num+1;
            tv_two.setText(num+"");
            Log.i("BroadcastReceiveTwo","广播接收者Two，收到了广播消息");
            delay();
        }
    }
    class MyBroadcastReceiveThree extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent){
            tv_three.setVisibility(View.VISIBLE);
            num=num+1;
            tv_three.setText(num+"");
            Log.i("BroadcastReceiveThree","广播接收者Three，收到了广播消息");
            delay();
        }
    }
    public void delay(){
        try{
            Thread.sleep(500);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }
    public void registerReceiver(){
        one=new MyBroadcastReceiveOne();
        IntentFilter filter1=new IntentFilter();
        filter1.setPriority(1000);
        filter1.addAction("Count_Ducks");
        registerReceiver(one,filter1);
        two=new MyBroadcastReceiveTwo();
        IntentFilter filter2=new IntentFilter();
        filter2.setPriority(200);
        filter2.addAction("Count_Ducks");
        registerReceiver(two,filter2);
        three=new MyBroadcastReceiveThree();
        IntentFilter filter3=new IntentFilter();
        filter3.setPriority(600);
        filter3.addAction("Count_Ducks");
        registerReceiver(three,filter3);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(one);
        unregisterReceiver(two);
        unregisterReceiver(three);
    }
}